// $Id: $

var apmap = {

  init: function() {
    $('#awplus_content table:eq(0) a').each(function() {
      
      var id = this.href.match(/([0-9]+)$/)[1];
      this.id = id;
      this.href = "javascript:void(0);";

      $(this).click(apmap.attachClick);
    });
  },

  initPlayer: function(e) {
    var pid = this.href.match(/([0-9]+)$/)[1];
    this.id = pid;
    $(this).addClass('applayer');
    this.href = "javascript:void(0);";

    $(this).click(apmap.attachPlayerClick);   
  },

  getSystemData: function(e) {
    var id = this.id;
    var pai = this;

    $('#apsys_'+ id).toFront().html('Loading...');

    $.get('Detail.php/?nr='+id,
      function(data) {
        var syshtml = data.replace(/\n/g, '');
        var aphtml = '<div id="apsys_'+id+'_title" style="background: #202060; padding: 3px; cursor: move; text-align: right;"><span id="apsys_'+id+'_reload" style="cursor: pointer;">Reload</span> | <span id="apsys_'+id+'_close" style="cursor: pointer;">Close</span></div>\n';
        aphtml += '<table width="100%" style="padding: 10px;">';
        aphtml += $('table:eq(2)', syshtml).html();
        aphtml += '</table>';

        $('#apsys_'+ id)
          .html(aphtml)
          .Draggable({handle: $('#apsys_'+id+'_title')[0]})
          .find('table tr:eq(0) td')
            .append('&nbsp;['+id+']')
          .end()
          .find('table tr:gt(1) td a')
            .each(apmap.initPlayer)
          .end()
          .click(function() {$(this).toFront();});

        $('#apsys_'+ id+'_close').click(function() {
          $('#apsys_'+ id).toggle();
        });

        $('#apsys_'+ id+'_reload').click(function() {
          $('#apsys_'+id).empty();
          pai.getSystemData();
        });
      });
  },

  getPlayerData: function(e) {
    var id = this.id;
    var pai = this;

    $('#appla_'+ id).toFront().html('Loading...');

    $.get('/0/Player/Profile.php/?id='+id,
      function(data) {
        var plahtml = data.replace(/\n/g, '');
        var aphtml = '<div id="appla_'+id+'_title" style="background: #026060; padding: 3px; cursor: move; text-align: right;"><span id="appla_'+id+'_reload" style="cursor: pointer;">Reload</span> | <span id="appla_'+id+'_close" style="cursor: pointer;">Close</span></div>\n';
        aphtml += '<table width="100%" style="padding: 10px;">';
        aphtml += $('table:eq(2)', plahtml).html();
        aphtml += '</table>';

        $('#appla_'+ id)
          .html(aphtml)
          .Draggable({handle: $('#appla_'+id+'_title')[0]})
          .find('table:eq(1) tr:eq(0) td center')
            .append('&nbsp;['+id+']')
          .end()
          .click(function() {$(this).toFront();})
          .toFront();

        if ($('#appla_'+id+' table:eq(0) tr:eq(0) > td').size() == 2) {
          $('#appla_'+id).css('width', 490);
        }

        $('#appla_'+ id+'_close').click(function() {
          $('#appla_'+ id).toggle();
        });

        $('#appla_'+ id+'_reload').click(function() {
          $('#appla_'+id).empty();
          pai.getPlayerData();
        });
      });   
  },

  attachClick: function(e) {
    var id = this.id;
    if ($('#apsys_'+ id).show().toFront().size()) { return; }

    $('body').append('<div id="apsys_'+id+'" class="apsys"></div>');

    var pos = findPos($('body table:eq(2)')[0]);

    var left = (e.clientX - 150 < 0) ? 0 : e.clientX - 150;
    var top = (e.clientY - 160 < 0) ? 0 : e.clientY - 160;

    $('#apsys_'+ id)
      .toFront()
      .css({
        'position': 'absolute',
        'left': left,
        'top': top,
        'width': 300,
        'border': '2px solid black',
        'background': '#191919',
      });

    this.getSystemData = apmap.getSystemData;
    this.getSystemData();
  },

  attachPlayerClick: function(e) {
     var id = this.id;
    if ($('#appla_'+ id).show().toFront().size()) { return; }

    $('body').append('<div id="appla_'+id+'" class="appla"></div>');

    var pos = findPos($('body table:eq(2)')[0]);

    var left = (e.clientX - 150 < 0) ? 0 : e.clientX - 150;
    var top = (e.clientY - 160 < 0) ? 0 : e.clientY - 160;

    $('#appla_'+ id)
      .toFront()
      .css({
        'position': 'absolute',
        'left': left,
        'top': top,
        'width': 270,
        'border': '2px solid black',
        'background': '#191919',
      });

    this.getPlayerData = apmap.getPlayerData;
    this.getPlayerData();   
  }
};

var jq_zindex = 100;
jQuery.fn.toFront = function() {
  return this.each(function() {
    $(this).css('z-index', ++jq_zindex);
  });
}

$(document).ready(function () {

  apmap.init();

  var html = '<div id="ap_findsid" align="center" style="margin: 5px;">'
  html += 'System ID: ';
  html += '<input id="ap_sid" type="text" size="3" style="color: black;" /> ';
  html += '<input id="ap_sid_find" type="button" value="Find" style="color: black;" />';
  html += '</div>';

  $('table:eq(2)').before(html);

  $('#ap_sid').keyup(function(e) {
    if (e.keyCode == 13) {
      $('#ap_sid_find').trigger('click');
    }
  })[0].focus();

  $('#ap_sid_find').click(function() {
    var sid = $('#ap_sid').val();
    var sys = $('#'+ sid);

    if (!sys.size()) {
      alert('System #'+sid+' not found!');
      return;
    }

    sys.ScrollTo().Shake(5).Pulsate();
  });

});
